package ais.dcproject.action;

import java.sql.Date;
import java.sql.Timestamp;

import javax.servlet.http.HttpServletRequest;

import ais.dcproject.constant.Constant;
import ais.dcproject.model.ApplyInfo;
import ais.dcproject.model.User;
import ais.dcproject.service.ApplyService;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class ApplyAction extends ActionSupport{
	
	private ApplyService applyService;
	
	private ApplyInfo applyInfo;
	private User user;
	
	public void setApplyService(ApplyService applyService) {
		this.applyService = applyService;
	}
	

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public ApplyInfo getApplyInfo() {
		return applyInfo;
	}

	public void setApplyInfo(ApplyInfo applyInfo) {
		this.applyInfo = applyInfo;
	}
	
	public String saveOrUpdate() throws Exception{
		boolean isNew = (Boolean) ActionContext.getContext().getSession().get(Constant.IS_NEW);
		applyInfo.setApplyDate(new Date(System.currentTimeMillis()));
		System.out.println("New   "  + applyInfo.toString());
		System.out.println(isNew);
		if(isNew){
			applyService.addNewApplyInfo(applyInfo);
			ActionContext.getContext().getSession().put(Constant.IS_NEW, false);
		}else{
			applyService.updateApplyInfo(applyInfo);
		}
		return SUCCESS;
	}

	public String execute() throws Exception {
		user = (User) ActionContext.getContext().getSession().get("user");
		applyInfo = applyService.getApplyInfo(user.getStuNum(), user.getIdCard());
		boolean isNew = false;
		if(null == applyInfo){
			isNew = true;
			applyInfo = new ApplyInfo();
			applyInfo.setStuNum(user.getStuNum());
			applyInfo.setIdCard(user.getIdCard());
			applyInfo.setName(user.getName());
			applyInfo.setDepartmentId(1);
		}
		
		applyInfo.setLastModifyTime(new Timestamp(System.currentTimeMillis()));
		System.out.println(applyInfo.toString());
		
		ActionContext.getContext().getSession().put(Constant.IS_NEW, isNew);
		return SUCCESS;
	}
}
